|
|
|
|
|
|
|
You can use the ListBox control to present a list of choices. Figure 4.9 shows a simple list used to pick a state abbreviation for use on a mailing label. This list shows all the components that make up the list box. |
|
|
|
|
|
|
|
|
Figure 4.9
A simple list box contains a
series of choices for the users. |
|
|
|
|
|
|
|
|
The key parts of the list box are the following: |
|
|
|
|
|
|
|
|
Item list. This is the list of items from which the users can select. These items are added to the list in the design environment or by your program as it is running. |
|
|
|
|
|
|
|
|
Selected item. This item is chosen by the users. Depending on the style of list you choose, a selected item is indicated by a highlight bar or by a check in the box next to the item. |
|
|
|
|
|
|
|
|
Scrollbar. This part indicates that more items are available on the list than will fit in the box and provides the users with an easy way to view the additional items. |
|
|
|
|
|
|
|
|
To the users, using the list box is similar to choosing channels on a TV. The cable company decides which channels to put on the selection list. The customers then can pick any of these channels but can't add one to the list if they don't like any of the choices provided. With the list box, the choices are set up by you, the programmer; the users can select only from the items you decide should be available. |
|
|
|
|
|
|
|
|
When you first draw a list box on the form, it shows only the border of the box and the text List1 (the name of the list box). No scrollbar is present and, of course, no list items are available. A vertical scrollbar is added to the list box automatically when you list more items than fit in the box. Note that the list box doesn't have a horizontal scrollbar if the choices are too wide for the control, so you should make sure that the list box is wide enough to display all of its entries. |
|
|
|
|
|
|
|
|
Basic Use of the List Box |
|
|
|
|
|
|
|
|
The simplest way to control the choices available in a list box is to use the AddItem method to add items to the list box. The only thing you need to specify is the text that you want to be placed in the list box. To try it out, start a new Standard EXE project, draw a list box on the form, and add the following lines of code to the form's Load event: |
|
|
|
|
|
|
|
|
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 100
List1.AddItem "This is item " & i
Next i
End Sub |
|
|
|
|
|